﻿Imports HslCommunication
Imports HslCommunication.Profinet.Melsec
Imports HslControls
Public Class ucLHJ
    Private _status As LHJ_STATUS
    Private _row As Integer
    Private _col As Integer
    Private _equipID As String
    Private _prodID As String
    Private _RowCol As String
    Private _info As String

    '-------------
    Private _serverIp As String
    Private _serverPort As String
    Private _lhjIp As String
    Private _lhjPort As String
    Private _spec_ID As String

    Private _plcDataAddress As String = GetIniFile("PLC", "plcDataAddress", "", g_szInitFileName) '温度14个，压力1个
    Private _plcLHStatus As String = GetIniFile("PLC", "plcLHStatus", "", g_szInitFileName)   '处于第几个硫化区间
    Private _plcLHDtLen As String = GetIniFile("PLC", "plcLHDtLen", "", g_szInitFileName)     '某个区间的加热时长
    Private _plcSYSJ As String = GetIniFile("PLC", "plcSYSJ", "", g_szInitFileName) '剩余时长
    Private _plcZLHSJ As String = GetIniFile("PLC", "plcZLHSJ", "", g_szInitFileName)  '总硫化时长
    Private _PlcZSC As String = GetIniFile("PLC", "PlcZSC", "", g_szInitFileName)  '总计时
    Private _PlcSMYL As String = GetIniFile("PLC", "plcSMYL", "", g_szInitFileName)  '总计时

    Private _interval As Integer
    Dim fx3uPLC As MelsecA1ENet
    '-------------
    Private _fatherOBJ As frmLHJList
    Private objEquip As New clsHWSetting

    Public _ParentForm As Form
    Public Sub New(status As LHJ_STATUS, row As String, col As String, Optional EquipID As String = "",
                   Optional prodid As String = "", Optional info As String = "")
        InitializeComponent()
        Me.Status = status
        Me.EquipID = EquipID
        'Me.lblRowCol.Text = EquipID '"  " & col & "-" & row
        'Me.Info = info
        HslBadge1.Text = EquipID
        'HslBadge1.RightText = info
        If prodid <> "" Then Me.ProdID = prodid
        'RowCol = Me.lblRowCol.Text
        TmDB.Start()

    End Sub

    Public Sub check()
        fx3uPLC = New MelsecA1ENet(_lhjIp, _lhjPort)
        If Status = LHJ_STATUS.LOSE_CONNECT Then
            If fx3uPLC.IpAddressPing = Net.NetworkInformation.IPStatus.Success Then
                If ProdID <> "" Then
                    objEquip.setLHJStatus(EquipID, LHJ_STATUS.BUSY)
                Else
                    objEquip.setLHJStatus(EquipID, LHJ_STATUS.EMPTY)
                End If
            End If
        End If
        If fx3uPLC.IpAddressPing <> Net.NetworkInformation.IPStatus.Success Then
            objEquip.setLHJStatus(EquipID, LHJ_STATUS.LOSE_CONNECT)
        End If
    End Sub

    Public Property Status As LHJ_STATUS
        Get
            Return _status
        End Get
        Set(value As LHJ_STATUS)
            _status = value
            Select Case value
                Case LHJ_STATUS.BUSY
                    'BackColor = Color.Green
                    'lblLHJ_No.ForeColor = Color.White
                    'lblRowCol.ForeColor = Color.White

                    HslBadge1.RightBackground = Color.Green
                    Info = "硫化中"
                Case LHJ_STATUS.EMPTY
                    'BackColor = Color.White
                    'lblLHJ_No.ForeColor = Color.Black
                    'lblRowCol.ForeColor = Color.Black
                    HslBadge1.RightBackground = Color.Silver
                    Info = "无任务"
                Case LHJ_STATUS.LOSE_CONNECT
                    'BackColor = Color.Silver
                    'lblLHJ_No.ForeColor = Color.White
                    'lblRowCol.ForeColor = Color.White
                    HslBadge1.RightBackground = Color.Brown
                    Info = "无法连接"
                Case LHJ_STATUS.WAIT_OUT
                    'BackColor = Color.Yellow
                    'lblLHJ_No.ForeColor = Color.Red
                    'lblRowCol.ForeColor = Color.Red
                    HslBadge1.RightBackground = Color.Blue
                    Info = "要出炉"
                Case LHJ_STATUS.ERR
                    'BackColor = Color.Red
                    'lblLHJ_No.ForeColor = Color.White
                    'lblRowCol.ForeColor = Color.White
                    HslBadge1.RightBackground = Color.Red
                    Info = "异常"
                Case LHJ_STATUS.REPAIRS
                    'BackColor = Color.Blue
                    'lblLHJ_No.ForeColor = Color.White
                    'lblRowCol.ForeColor = Color.White
                    HslBadge1.RightBackground = Color.DarkOrange
                    Info = "维修中"
            End Select
        End Set
    End Property

    Public Property Row As String
        Get
            Return _row
        End Get
        Set(value As String)
            _row = value
        End Set
    End Property

    Public Property Col As String
        Get
            Return _col
        End Get
        Set(value As String)
            _col = value
        End Set
    End Property

    Public Property EquipID As String
        Get
            Return _equipID
        End Get
        Set(value As String)
            _equipID = value
        End Set
    End Property

    Public Property RowCol As String
        Get
            Return _RowCol
        End Get
        Set(value As String)
            _RowCol = value
        End Set
    End Property

    Public Property Info As String
        Get
            Return _info
        End Get
        Set(value As String)
            _info = value
            HslBadge1.RightText = value
            'lblLHJ_No.Text = value
        End Set
    End Property

    Public Property ProdID As String
        Get
            Return _prodID
        End Get
        Set(value As String)
            _prodID = value

        End Set
    End Property
    Public Property Interval As Integer
        Get
            Return _interval
        End Get
        Set(value As Integer)
            _interval = value

            If value > 0 Then
                tmPLC.Interval = value
                readPLCandSaveData()
                tmPLC.Start()
            Else
                tmPLC.Stop()
            End If
        End Set
    End Property

    Public Property LhjIp As String
        Get
            Return _lhjIp
        End Get
        Set(value As String)
            _lhjIp = value
        End Set
    End Property

    Public Property LhjPort As Integer
        Get
            Return _lhjPort
        End Get
        Set(value As Integer)
            _lhjPort = value
        End Set
    End Property

    Public Property ServerIp As String
        Get
            Return _serverIp
        End Get
        Set(value As String)
            _serverIp = value
        End Set
    End Property

    Public Property ServerPort As String
        Get
            Return _serverPort
        End Get
        Set(value As String)
            _serverPort = value
        End Set
    End Property

    Public Property Spec_ID As String
        Get
            Return _spec_ID
        End Get
        Set(value As String)
            _spec_ID = value
        End Set
    End Property

    Enum LHJ_STATUS
        EMPTY = 0 '空闲
        BUSY = 1 '工作中
        LOSE_CONNECT = 2 '失联
        WAIT_OUT = 3 '等待出炉
        ERR = 4 '有错误
        REPAIRS = 5 '维修
    End Enum

    Private Sub tmPLC_Tick(sender As Object, e As EventArgs) Handles tmPLC.Tick
        readPLCandSaveData()
    End Sub

    Public Sub readLHJStatusFromDB()
        Dim objHWSetting As New clsHWSetting
        Dim _itl As Integer
        Dim dt As DataTable = objHWSetting.selectForLHJByID(g_ws_lh, _equipID)
        If dt.Rows.Count > 0 Then
            If dt.Rows(0)("status") = "5" Then
                _itl = 30 * 1000
                Status = LHJ_STATUS.REPAIRS
                If tmPLC.Interval <> _itl Or tmPLC.Enabled = False Then

                    tmPLC.Stop()
                    tmPLC.Interval = _itl
                    tmPLC.Start()
                End If
            End If
            If DBNULLtoBlank(dt.Rows(0)("prod_id")) = "" Or dt.Rows(0)("status") = "0" Then
                Status = LHJ_STATUS.EMPTY
                tmPLC.Stop()
            Else

                ProdID = DBNULLtoBlank(dt.Rows(0)("prod_id"))
                Spec_ID = DBNULLtoBlank(dt.Rows(0)("prod_spec_id"))
                _itl = objHWSetting.getSpecIntervalbySpecID(Spec_ID)
                If dt.Rows(0)("status") = "1" Then
                    If DBNULLtoBlank(dt.Rows(0)("last_minutes")) <> "" AndAlso CInt(dt.Rows(0)("last_minutes")) <= 20 Then
                        _itl = 60 * 1000 '小于20分钟时，1分钟监听一次
                    End If
                    Status = LHJ_STATUS.BUSY
                End If
                If dt.Rows(0)("status") = "2" Then
                    _itl = 5 * 1000
                    Status = LHJ_STATUS.LOSE_CONNECT
                End If
                If dt.Rows(0)("status") = "3" Then
                    Status = LHJ_STATUS.WAIT_OUT
                End If
                If dt.Rows(0)("status") = "4" Then
                    ' _itl = 30 * 1000
                    Status = LHJ_STATUS.ERR
                End If
                If tmPLC.Interval <> _itl Or tmPLC.Enabled = False Then

                    tmPLC.Stop()
                    tmPLC.Interval = _itl
                    readPLCandSaveData()
                    tmPLC.Start()
                End If
            End If


        End If
    End Sub
    Public Sub readPLCandSaveData()

        'Dim labelFontColor As Color = lblLHJ_No.ForeColor
        Dim objLHJ As New clsLHJWorkDetail

        If _lhjIp = "" Then Exit Sub
        fx3uPLC = New MelsecA1ENet(_lhjIp, _lhjPort)

        Dim plcConnectFLg As OperateResult
        plcConnectFLg = New OperateResult

        Try

            If fx3uPLC.IpAddressPing <> Net.NetworkInformation.IPStatus.Success Then
                plcConnectFLg.IsSuccess = False
                Status = LHJ_STATUS.LOSE_CONNECT
                objEquip.setLHJStatus(EquipID, LHJ_STATUS.LOSE_CONNECT)
                'lblLHJ_No.ForeColor = Color.Purple
                'Me.ParentForm.Refresh()
                Exit Sub
            End If
            If ProdID = "" Then Exit Sub
            plcConnectFLg = fx3uPLC.ConnectServer

            If plcConnectFLg.IsSuccess = True Then

                '读取寄存地址内的数据 BEGIN----------

                '温度20个，压力1个
                Dim plcDataRes As OperateResult(Of Short()) = fx3uPLC.ReadInt16(_plcDataAddress, 14)
                Dim plcData(13) As Double

                System.Threading.Thread.Sleep(100)
                '处于第几个硫化区间
                Dim lhStatus As Short = fx3uPLC.ReadInt16(_plcLHStatus).Content

                System.Threading.Thread.Sleep(100)
                '某个区间的加热时长
                Dim lhDtLen As Short = fx3uPLC.ReadInt16(_plcLHDtLen).Content

                System.Threading.Thread.Sleep(100)
                '剩余时间
                Dim _lhsysj As Short '= fx3uPLC.ReadInt16(_plcSYSJ).Content

                'System.Threading.Thread.Sleep(100)
                '硫化总时长
                Dim _zsc As Short '= fx3uPLC.ReadInt16(_PlcZSC).Content

                'System.Threading.Thread.Sleep(100)
                '硫化时长  
                Dim _lhjsc As Short '= fx3uPLC.ReadInt16(_plcZLHSJ).Content

                Dim plcTimeDataRes As OperateResult(Of Short()) = fx3uPLC.ReadInt16("D253", 3)
                If plcTimeDataRes.IsSuccess = True Then
                    _zsc = plcTimeDataRes.Content(0)
                    _lhsysj = plcTimeDataRes.Content(1)
                    _lhjsc = plcTimeDataRes.Content(2)
                End If
                System.Threading.Thread.Sleep(100)
                Dim _smyl As Short = fx3uPLC.ReadInt16(_PlcSMYL).Content

                '读取寄存地址内的数据 END----------
                Dim enLH As New entity_LHDataInput

                If plcDataRes.IsSuccess = True Then
                    '温度14个，压力1个
                    For i As Integer = 0 To plcData.Length - 1
                        plcData(i) = plcDataRes.Content(i)
                    Next

                    With enLH
                        .col_time = Now
                        .lhj_id = EquipID
                        .factory_id = g_factory_id
                        'PLC中的20个温度
                        .temp_val_1 = plcData(0) '上1
                        .temp_val_2 = plcData(1) '上2
                        .temp_val_3 = plcData(2) '上3
                        .temp_val_4 = plcData(3) '上4
                        .temp_val_5 = plcData(4) '下1
                        .temp_val_6 = plcData(5) '下2
                        .temp_val_7 = plcData(6) '下3
                        .temp_val_8 = plcData(7) '下4
                        .temp_val_9 = plcData(8) '中1
                        .temp_val_10 = plcData(9) '中2
                        .temp_val_11 = plcData(10) '中3
                        .temp_val_12 = plcData(11) '中4

                        .temp_val_13 = 0
                        .temp_val_14 = 0
                        .temp_val_15 = 0
                        .temp_val_16 = 0
                        .temp_val_17 = 0
                        .temp_val_18 = 0
                        .temp_val_19 = 0
                        .temp_val_20 = 0

                        'PLC中的压力
                        .pres_val1 = _smyl / 10 '锁模压力
                        .pres_val2 = plcData(13) / 10 '主缸压力

                        'PLC中的硫化时长
                        .lh_time_len = lhDtLen

                        .lhjsc = _lhjsc
                        .lhsysj = _lhsysj
                        .zsc = _zsc
                        .old_status = lhStatus
                        If lhStatus = 0 Or lhStatus = 1 Then
                            .status = lhStatus
                        ElseIf lhStatus = 2 Or lhStatus = 3 Then
                            .status = 2
                        ElseIf lhStatus = 4 Or lhStatus = 5 Then
                            .status = 3
                        End If
                        .prod_id = _prodID
                        .update_time = Now

                    End With
                    objLHJ.insertData(enLH)
                    objEquip.setLHJSYSJ(EquipID, _lhsysj)
                    objEquip.setProdInfoTime(_prodID, _lhjsc, _zsc)
                    setBusy(enLH, _lhsysj) '不把温度、压力作为异常的判断标准
                    'Status = LHJ_STATUS.BUSY
                    'objEquip.setLHJStatus(EquipID, Status)
                    '_ParentForm.Refresh()
                    fx3uPLC.ConnectClose()
                End If
            Else
                plcConnectFLg.IsSuccess = False
                Status = LHJ_STATUS.LOSE_CONNECT
                objEquip.setLHJStatus(EquipID, LHJ_STATUS.LOSE_CONNECT)
                Exit Sub
            End If

        Catch ex As Exception
            plcConnectFLg.IsSuccess = False
            '连接失败
            Status = LHJ_STATUS.LOSE_CONNECT
            objEquip.setLHJStatus(EquipID, LHJ_STATUS.LOSE_CONNECT)
        Finally
            If IsNothing(plcConnectFLg) = False AndAlso plcConnectFLg.IsSuccess = True Then
                fx3uPLC.ConnectClose()
            End If
        End Try
    End Sub

    Private Sub setBusy(en As entity_LHDataInput, _sysj As Short)
        Dim objProdSpec As New clsProdSpec
        'Dim statusSum As Integer = 0 '总工有几个status
        Dim stStr As LHJ_STATUS = LHJ_STATUS.BUSY

        '根据prod_id取得支座型号信息
        Dim dtSpec As DataTable = objProdSpec.getProdSpecInfo(ProdID)

        If dtSpec.Rows.Count > 0 Then
            Dim dr As DataRow = dtSpec.Rows(0)

            'If CDbl(DBNULLtoZero(dr("temp_st_val_1"))) > 0 Then statusSum = 1
            'If CDbl(DBNULLtoZero(dr("temp_st_val_2"))) > 0 Then statusSum = 2
            'If CDbl(DBNULLtoZero(dr("temp_st_val_3"))) > 0 Then statusSum = 3

            '当是最终状态的时候，判断是否待出炉
            If _sysj <= 10 Then
                stStr = LHJ_STATUS.WAIT_OUT
            End If

            '判断温度or压力是否Error
            'If chkWD(en.temp_val_1, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_2, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_3, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_4, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_5, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_6, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_7, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_8, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_9, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_10, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If

            'If chkWD(en.temp_val_11, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkWD(en.temp_val_12, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            ''If chkWD(en.temp_val_13, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_14, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_15, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_16, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_17, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_18, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_19, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            ''If chkWD(en.temp_val_20, dr("temp_max_val_" & en.status), dr("temp_min_val_" & en.status)) = False Then
            ''    stStr = LHJ_STATUS.ERR
            ''End If
            'If chkYL(en.pres_val1, dr("pres_max_val_" & en.status & "1")， dr("pres_min_val_" & en.status & "2")) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
            'If chkYL(en.pres_val2, dr("pres_max_val_" & en.status & "1")， dr("pres_min_val_" & en.status & "2")) = False Then
            '    stStr = LHJ_STATUS.ERR
            'End If
        End If
        Status = stStr
        objEquip.setLHJStatus(EquipID, stStr)
    End Sub

    Private Function chkWD(wd As Double, sj_wd_Max As Double, sj_wd_Min As Double) As Boolean
        If wd > 0 Then
            If wd > sj_wd_Max Or wd < sj_wd_Min Then
                Return False
            Else
                Return True
            End If
        Else
            Return True
        End If
    End Function

    Private Function chkYL(yl As Double, sj_yl_Max As Double, sj_yl_min As Double) As Boolean
        If yl > 0 Then
            If yl > sj_yl_Max Or yl < sj_yl_min Then
                Return False
            Else
                Return True
            End If
        Else
            Return True
        End If
    End Function


    'Private Sub readPLCandSaveData()
    '    Dim fx As New Mitsubishi_FX_ENET_TCP
    '    Dim objLHJ As New clsLHJWorkDetail
    '    fx.LocalIP = ServerIp
    '    fx.LocalIP = ServerPort
    '    fx.RemoteIP = LhjIp
    '    fx.RemotePort = LhjPort
    '    fx.doLink()
    '    Dim LhjInfo As String = fx.Read
    '    If LhjInfo <> "" Then

    '        Dim st = LhjInfo.Split("-")

    '        Dim enLH As New entity_LHDataInput
    '        With enLH
    '            .col_time = Now
    '            .lhj_id = EquipID
    '            .factory_id = g_factory_id
    '            'PLC中的9个温度
    '            .temp_val_1 = st(0)
    '            .temp_val_2 = st(1)
    '            .temp_val_3 = st(2)
    '            .temp_val_4 = st(3)
    '            .temp_val_5 = st(4)
    '            .temp_val_6 = st(5)
    '            .temp_val_7 = st(6)
    '            .temp_val_8 = st(7)
    '            .temp_val_9 = st(8)
    '            'PLC中的压力
    '            .pres_val = st(9)
    '            'PLC中的硫化时长
    '            .lh_time_len = st(10)
    '            .status = st(11)
    '            .prod_id = _prodID
    '            .update_time = Now
    '        End With
    '        fx.closeLink()
    '        objLHJ.insertData(enLH)
    '    End If
    'End Sub

    Private Sub TmDB_Tick(sender As Object, e As EventArgs) Handles TmDB.Tick
        readLHJStatusFromDB()
    End Sub

    Private Sub HslBadge1_Click(sender As Object, e As EventArgs) Handles HslBadge1.Click
        '硫化机组件点击事件
        If Status = LHJ_STATUS.BUSY Or Status = LHJ_STATUS.ERR Or Status = LHJ_STATUS.WAIT_OUT Then
            '只有状态“忙”的硫化机进行迁移
            '画面迁移
            Dim detail As New frmLHJDetail
            detail.LhjID = _equipID
            detail.ShowDialog()
        End If
    End Sub
End Class
